//
//  MODocMessageController.m
//  ZLFastDoctor
//
//  Created by 冯龙飞 on 16/5/24.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "MODocMessageController.h"
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>
#import "MOConcernModel.h"
#import <UIImageView+WebCache.h>
#import "UIView+XYFrame.h"
#import <RongIMKit/RongIMKit.h>
#import "MOChatListViewController.h"
#import <RongIMKit/RCConversationViewController.h>

@interface MODocMessageController ()<UIScrollViewDelegate>

// nav上右边按钮
@property(nonatomic, strong) UIButton * rightBtn;

#pragma mark -- 上部视图的内容
@property(nonatomic, strong) UIView * docMes;
@property(nonatomic, strong) UIImageView * icon;
@property(nonatomic, copy) UILabel * name;
@property(nonatomic, copy) UILabel * kind;
@property(nonatomic, copy) UILabel * hospital;
@property(nonatomic, strong) UIImageView * add;
@property(nonatomic, strong) UIImageView * flower;
@property(nonatomic, strong) UIImageView * collect;
@property(nonatomic, copy) UILabel * addCount;
@property(nonatomic, copy) UILabel * flowerCount;
@property(nonatomic, copy) UILabel * collectCount;
@property(nonatomic, strong) UIView * lineOne;
@property(nonatomic, strong) UIView * lineTwo;

#pragma mark -- 中间label的内容
@property(nonatomic, strong) UIView * midView;
@property(nonatomic, strong) UILabel * leftLabel;
@property(nonatomic, strong) UILabel * rightLabel;
@property(nonatomic, strong) UIView * midLine;
@property(nonatomic, strong) UIView * bottomLine;
@property(nonatomic, strong) UITapGestureRecognizer * leftTap;
@property(nonatomic, strong) UITapGestureRecognizer * rightTap;

#pragma mark -- 下面的scrollview和button
@property(nonatomic, strong) UIScrollView * scroll;
@property(nonatomic, strong) UIButton * askDoc;
@property(nonatomic, copy) UILabel * docData;
@property(nonatomic, copy) UILabel * onePage;
@property(nonatomic, copy) UILabel * twoPage;

@end

/*
 注意::: 控件用Masonry约束的时候必须先将控件用addSubview加载到父视图中才能进行约束，不然就会造成崩溃!!!!所以注意系统调用方法的顺序
 */

@implementation MODocMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = NQColor(242, 242, 242);
    
    // 在这里添加一个视图,用来装上面部分的内容
    [self.view addSubview:self.docMes];
    [self.view addSubview:self.midView];
    [self.view addSubview:self.askDoc];
    [self.view addSubview:self.scroll];
    
    // 设置上部视图
    [self setupUI];
    
    // 设置中间视图
    [self setupMidUI];
    
    // 设置下面的视图
    [self setupBottomUI];
}

// 设置下面视图的约束
- (void)setupBottomUI {
    
    [self.scroll addSubview:self.onePage];
    [self.scroll addSubview:self.twoPage];
    
    [self.askDoc makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.bottom.equalTo(self.view).offset(-5);
    }];
    
    [self.scroll makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.midView.bottom);
        make.bottom.equalTo(self.askDoc.top).offset(-5);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    
    [self.onePage makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.scroll);
        make.width.equalTo(self.midView);
    }];
    
    [self.twoPage makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scroll);
        make.left.equalTo(self.scroll).offset(self.midView.bounds.size.width);
        make.width.equalTo(self.midView);
    }];
    
    [self setValueToBottom];
}

// 设置scrollview和下面按钮的相关属性
- (void)setValueToBottom {
    
    [self.askDoc setBackgroundColor:NQNavColor];
    self.askDoc.layer.cornerRadius = 5;
    self.askDoc.layer.masksToBounds = YES;
    // 设置高亮状态文字颜色
    [self.askDoc setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    // 给按钮添加点击事件
    [self.askDoc setTitle:@"咨询医生" forState:UIControlStateNormal];
    
    [self.askDoc addTarget:self action:@selector(loginRongCloud) forControlEvents:UIControlEventTouchUpInside];
    
    self.onePage.text = @"污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........污污污污污污污污污污污..........";
    self.twoPage.text = @"毕业于\"XXOO医科大学\"";
}

/**
 *登录融云
 *
 */
-(void)loginRongCloud {
    NSLog(@"按钮点击了,跳转到即时通讯界面");
    
    NSString *token = @"WAu+OBzsVMhbEjoCCiQZ5hNFM7OUxN4zdnSbKuH+7DX2hwWgZEHzkK1tM0RMxN9osnEob4XX47+B1YGup2uk1YLu+HwS1u5J";
    
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
        //设置用户信息提供者,页面展现的用户头像及昵称都会从此代理取
        [[RCIM sharedRCIM] setUserInfoDataSource:self];
//        NSLog(@"Login successfully with userId: %@.", userId);
        dispatch_async(dispatch_get_main_queue(), ^{
            MOChatListViewController *chatListViewController = [[MOChatListViewController alloc]init];
            
            // 把模型数据传送过去
            chatListViewController.model = self.model;
            
            [self.navigationController pushViewController:chatListViewController animated:YES];
        });
        
    } error:^(RCConnectErrorCode status) {
        NSLog(@"login error status: %ld.", (long)status);
    } tokenIncorrect:^{
        NSLog(@"token 无效 ，请确保生成token 使用的appkey 和初始化时的appkey 一致");
    }];
    
}

/**
 *此方法中要提供给融云用户的信息，建议缓存到本地，然后改方法每次从您的缓存返回
 */
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion
{
    //此处貌似是推送过来的消息
    if ([@"zongshuai223" isEqual:userId]) {
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = @"zongshuai223";
        user.name = self.model.doctor_name;
        user.portraitUri = self.model.doctor_portrait;
        
        return completion(user);
    }else if([@"KEFU146423311116243" isEqual:userId]) {
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = @"KEFU146423311116243";
        user.name = @"客服";
        user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
        return completion(user);
    }
}

// 必须在这个方法中赋值,注意方法调用的先后顺序
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 赋值
    [self setValueToSubviews];
}

// 添加自定义视图上的子控件,并且布局
- (void)setupUI {
    
    [self.docMes addSubview:self.icon];
    [self.docMes addSubview:self.name];
    [self.docMes addSubview:self.kind];
    [self.docMes addSubview:self.hospital];
    [self.docMes addSubview:self.add];
    [self.docMes addSubview:self.flower];
    [self.docMes addSubview:self.collect];
    [self.docMes addSubview:self.addCount];
    [self.docMes addSubview:self.flowerCount];
    [self.docMes addSubview:self.collectCount];
    [self.docMes addSubview:self.lineOne];
    [self.docMes addSubview:self.lineTwo];
    
    [self setLayout];
}

- (void)setupMidUI {
    
    [self.midView addSubview:self.leftLabel];
    [self.midView addSubview:self.rightLabel];
    [self.midView addSubview:self.midLine];
    [self.midView addSubview:self.bottomLine];
    
    // 设置中间视图约束
    [self setupMidLayout];
}

- (void)setValueToSubviews {
    
    self.name.text = self.model.doctor_name;
    self.name.font = [UIFont systemFontOfSize:15];
    
    self.kind.text = self.model.doctor_title_name;
    self.kind.font = [UIFont systemFontOfSize:14];
//    self.kind.textColor = [UIColor grayColor];
    
    self.hospital.text = self.model.doctor_hospital_name;
    self.hospital.font = [UIFont systemFontOfSize:14];
//    self.hospital.textColor = [UIColor grayColor];
    
    self.addCount.text = [NSString stringWithFormat:@"预约量:%@",self.model.banner];
    self.addCount.font = [UIFont systemFontOfSize:12];
    
    self.flowerCount.text = [NSString stringWithFormat:@"鲜花量:%@",self.model.flower];
    self.flowerCount.font = [UIFont systemFontOfSize:12];
    
    self.collectCount.text = [NSString stringWithFormat:@"锦旗量:%@",self.model.operation_count];
    self.collectCount.font = [UIFont systemFontOfSize:12];
    
    [self.icon sd_setImageWithURL:[NSURL URLWithString:self.model.doctor_portrait]];
    
    // 判断,如果没有图片返回,就用占位图片代替
    if (!self.icon.image) {
        
        // 占位图片
        self.icon.image = [UIImage imageNamed:@"doctor_defaultphoto_male"];
    }
    
}

- (void)setLayout {
    
    // 上部视图的约束
    float margin = self.docMes.bounds.size.width * 0.33;
    float marginTwo = self.docMes.bounds.size.width * 0.66;
    
    [self.icon makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@70);
        make.top.left.equalTo(self.docMes).offset(8);
    }];
    
    [self.name makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.left.equalTo(self.icon.right).offset(8);
    }];
    
    [self.kind makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.name);
        make.left.equalTo(self.name.right).offset(8);
    }];
    
    [self.hospital makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.icon.centerY);
        make.left.equalTo(self.icon.right).offset(8);
    }];
    
    [self.add makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@10);
        make.bottom.equalTo(self.docMes).offset(-4);
        make.left.equalTo(self.docMes).offset(34);
    }];
    
    [self.addCount makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.add.right);
        make.bottom.equalTo(self.add.bottom).offset(2);
    }];
    
    [self.lineOne makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.height.equalTo(@14);
        make.bottom.equalTo(self.docMes.bottom).offset(-3);
        make.left.equalTo(margin);
    }];
    
    [self.flower makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineOne).offset(34);
        make.bottom.equalTo(self.add);
    }];
    
    [self.flowerCount makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.flower.right);
        make.bottom.equalTo(self.addCount);
    }];
    
    [self.lineTwo makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.height.equalTo(@14);
        make.bottom.equalTo(self.docMes.bottom).offset(-3);
        make.left.equalTo(marginTwo);
    }];
    
    [self.collect makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineTwo).offset(34);
        make.bottom.equalTo(self.add);
    }];
    
    [self.collectCount makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.collect.right);
        make.bottom.equalTo(self.addCount);
    }];
}

- (void)setupMidLayout {
    float labelMargin = self.view.bounds.size.width * 0.5 - 0.5;
//     中间label的视图
        [self.leftLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.midView);
            make.left.equalTo(self.midView);
            make.height.equalTo(@43);
            make.width.equalTo(labelMargin);
        }];
    
        [self.midLine makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@1);
            make.height.equalTo(@30);
            make.center.equalTo(self.midView);
        }];
    
        [self.rightLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.leftLabel);
            make.right.equalTo(self.midView);
            make.width.equalTo(labelMargin);
            make.height.equalTo(@43);
        }];
}

// 点击左边按钮会调用的方法
- (void)labelLeftTap {
    
    self.leftLabel.highlighted = YES;
    self.rightLabel.highlighted = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomLine.x = 5;
    }];
    // 联动,点击按钮的时候让scrollview滚动到相应位置
    [self.scroll setContentOffset:CGPointMake(0, 0) animated:YES];
}

// 点击右边按钮会调用的方法
- (void)labelRightTap {
    
    self.leftLabel.highlighted = NO;
    self.rightLabel.highlighted = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomLine.x = self.view.bounds.size.width * 0.5 + 5;
    }];
    // 联动,点击按钮的时候让scrollview滚动到相应位置
    [self.scroll setContentOffset:CGPointMake(self.midView.bounds.size.width, 0) animated:YES];
}

#pragma makr -- UIScrollView的代理方法
// 当停止滚动的时候调用这个方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"%f---%f",self.scroll.contentOffset.x,self.midView.bounds.size.width);
    if (self.scroll.contentOffset.x == 0) {
        
        [self labelLeftTap];
    }else if (self.scroll.contentOffset.x == self.midView.bounds.size.width) {
        
        [self labelRightTap];
    }
}

// 上部视图的整个view
- (UIView *)docMes {
    
    if (_docMes == nil) {
        _docMes = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
        _docMes.backgroundColor = [UIColor whiteColor];
    }
    return _docMes;
}

- (UIImageView *)icon {
    
    if (_icon == nil) {
        _icon = [[UIImageView alloc]init];
    }
    return _icon;
}

- (UILabel *)name {
    
    if (_name == nil) {
        _name = [[UILabel alloc]init];
    }
    return _name;
}

- (UILabel *)kind {
    
    if (_kind == nil) {
        _kind = [[UILabel alloc]init];
    }
    return _kind;
}

- (UILabel *)hospital {
    
    if (_hospital == nil) {
        _hospital = [[UILabel alloc]init];
    }
    return _hospital;
}

- (UIImageView *)add {
    
    if (_add == nil) {
        _add = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yuyueliang"]];
    }
    return _add;
}

- (UILabel *)addCount {
    
    if (_addCount == nil) {
        _addCount = [[UILabel alloc]init];
    }
    return _addCount;
}

- (UIImageView *)flower {
    
    if (_flower == nil) {
        _flower = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xianhua"]];
    }
    return _flower;
}

- (UILabel *)flowerCount {
    
    if (_flowerCount == nil) {
        _flowerCount = [[UILabel alloc]init];
    }
    return _flowerCount;
}

- (UIImageView *)collect {
    
    if (_collect == nil) {
        _collect = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"jinqi"]];
    }
    return _collect;
}

- (UILabel *)collectCount {
    
    if (_collectCount == nil) {
        _collectCount = [[UILabel alloc]init];
    }
    return _collectCount;
}

- (UIView *)lineOne {
    
    if (_lineOne == nil) {
        _lineOne = [[UIView alloc]init];
        _lineOne.backgroundColor = [UIColor grayColor];
    }
    return _lineOne;
}

- (UIView *)lineTwo {
    
    if (_lineTwo == nil) {
        _lineTwo = [[UIView alloc]init];
        _lineTwo.backgroundColor = [UIColor grayColor];
    }
    return _lineTwo;
}

- (UIView *)midView {
    
    if (_midView == nil) {
        _midView = [[UIView alloc]initWithFrame:CGRectMake(0, self.docMes.bounds.size.height + 10, self.view.bounds.size.width, 45)];
        _midView.backgroundColor = [UIColor whiteColor];
    }
    return _midView;
}

- (UILabel *)leftLabel {
    
    if (_leftLabel == nil) {
        _leftLabel = [[UILabel alloc]init];
        _leftLabel.text = @"接诊条件";
        _leftLabel.textAlignment = NSTextAlignmentCenter;
        _leftLabel.userInteractionEnabled = YES;
//        _leftLabel.backgroundColor = [UIColor whiteColor];
        _leftLabel.highlightedTextColor = NQNavColor;
        _leftLabel.highlighted = YES;
        // 给label添加点击手势
        [_leftLabel addGestureRecognizer:self.leftTap];
    }
    return _leftLabel;
}

- (UILabel *)rightLabel {
    
    if (_rightLabel == nil) {
        _rightLabel = [[UILabel alloc]init];
        _rightLabel.text = @"医生简介";
        _rightLabel.textAlignment = NSTextAlignmentCenter;
        _rightLabel.userInteractionEnabled = YES;
        _rightLabel.highlightedTextColor = NQNavColor;
//        _rightLabel.backgroundColor = [UIColor whiteColor];
        // 给label添加点击手势
        [_rightLabel addGestureRecognizer:self.rightTap];
    }
    return _rightLabel;
}

- (UIView *)midLine {
    
    if (_midLine == nil) {
        _midLine = [[UIView alloc]init];
        _midLine.backgroundColor = [UIColor grayColor];
    }
    return _midLine;
}

- (UIView *)bottomLine {
    
    if (_bottomLine == nil) {
        _bottomLine = [[UIView alloc]initWithFrame:CGRectMake(5, self.midView.bounds.size.height - 2, self.midView.bounds.size.width * 0.5 - 10, 2)];
        _bottomLine.backgroundColor = NQNavColor;
    }
    return _bottomLine;
}

- (UITapGestureRecognizer *)leftTap {
    
    if (_leftTap == nil) {
        _leftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelLeftTap)];
    }
    return _leftTap;
}

- (UITapGestureRecognizer *)rightTap {
    
    if (_rightTap == nil) {
        _rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelRightTap)];
    }
    return _rightTap;
}

- (UIButton *)askDoc {
    
    if (_askDoc == nil) {
        _askDoc = [[UIButton alloc]init];
    }
    return _askDoc;
}

- (UIScrollView *)scroll {
    
    if (_scroll == nil) {
        _scroll = [[UIScrollView alloc]init];
        _scroll.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 100);
        // 设置相关属性
        _scroll.backgroundColor = [UIColor whiteColor];
        _scroll.bounces = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.pagingEnabled = YES;
        // 设置代理
        _scroll.delegate = self;
    }
    return _scroll;
}

- (UILabel *)onePage {
    
    if (_onePage == nil) {
        _onePage = [[UILabel alloc]init];
        _onePage.userInteractionEnabled = YES;
        _onePage.numberOfLines = 0;
    }
    return _onePage;
}

- (UILabel *)twoPage {
    
    if (_twoPage == nil) {
        _twoPage = [[UILabel alloc]init];
        _onePage.userInteractionEnabled = YES;
        _twoPage.numberOfLines = 0;
    }
    return _twoPage;
}

@end
